<?php
namespace app\mall\logic;

use think\facade\Db;

class couponLogic{
    //优惠券数量小，不做分页了。

    /**
     * 领券中心
     */
    public function receivable_coupon(){
        return Db::table('one_coupon')
            ->where(array(
                array('status','=',1),
                array('expire_time','>=',date('Y-m-d H:i:s'))
            ))->order('create_time','desc')
            ->withoutField('status ,up_time ,create_time')
            ->select();
    }

    /**
     * 领取优惠券
     * @param int $uid 用户id
     * @param int $id 优惠券id
     * @return array
     */
    public function receive_coupon(int $uid ,int $id):array{
        $coupon_info = Db::table('one_coupon')
            ->where('id','=',$id)
            ->find();//优惠券信息
        if (empty($coupon_info)) return array('code'=>500,'msg'=>'优惠券不存在');
        if (strtotime($coupon_info['expire_time']) <time()) return array('code'=>500,'msg'=>'优惠券已过期');
        if (empty($coupon_info['status'])) return array('code'=>500,'msg'=>'优惠券已下架');
        if (empty($coupon_info['surplus_number'])) return array('code'=>500,'msg'=>'优惠券已经被领完了');
        if (!empty($coupon_info['each_number'])) {
            $user_receive = Db::table('one_user_coupon')
                ->where(array(
                    'u_id' => $uid,
                    'coupon_id' => $id
                ))->count();
            if ($user_receive >= $coupon_info['each_number']) return array('code'=>500,'msg'=>'您领取该优惠券的数量已达上限');
        }
        Db::startTrans();//开启事务
        $res1 = Db::table('one_user_coupon')
            ->insert(array(
                'u_id' => $uid,
                'coupon_id' => $id
            ));
        $res2 = Db::table('one_coupon')
            ->where('id','=',$id)
            ->dec('surplus_number')
            ->update();
        if ($res1 && $res2){
            Db::commit();
            return array('code'=>200,'msg'=>'优惠券领取成功');
        }
        Db::rollback();
        return array('code'=>500,'msg'=>'优惠券领取失败');
    }

    /**
     * 我的优惠券列表
     * @param int $uid 用户id
     * @param int $type 0待使用 1已使用 2已过期
     */
    public function coupon_list(int $uid ,int $type){
        $where = array(
            array('u_id','=',$uid)
        );
        switch (true){
            case $type == 0:
                $where[] = array('is_use','=',0);
                $where[] = array('expire_time','>=',date('Y-m-d H:i:s'));
                break;
            case $type == 1:
                $where[] = array('is_use','=',1);
                break;
            case $type == 2:
                $where[] = array('expire_time','<=',date('Y-m-d H:i:s'));
                break;
            default :
                break;
        }
        return Db::view('one_user_coupon','id ,is_use')
            ->where($where)
            ->view('one_coupon','coupon_name ,satisfy_money ,reduce_money ,expire_time','one_user_coupon.coupon_id = one_coupon.id')
            ->order('one_user_coupon.create_time','desc')
            ->select();
    }

    /**
     * 付款时可使用的优惠券
     * @param int $uid 用户id
     * @param int|float $money 订单价格
     */
    public function available_coupon(int $uid ,$money){
        $where = array(
            array('u_id','=',$uid),
            array('is_use','=',0),
            array('expire_time','>=',date('Y-m-d H:i:s')),
            array('satisfy_money','<=',$money)
        );
        return Db::view('one_user_coupon','id ,is_use')
            ->where($where)
            ->view('one_coupon','coupon_name ,satisfy_money ,reduce_money ,expire_time','one_user_coupon.coupon_id = one_coupon.id')
            ->order('one_user_coupon.create_time')
            ->select();
    }

}